/*********************************************************
 * copyright learnren.com 版权所有 
 * 开发人员：ivan.yu
 * 创建时间：2013/4/28 11:56:14
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using LEARNREN.COM.Entity;
using LEARNREN.COM.DAL;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.BLL
{
    public class ArticleLogic : LogicBase<Article>
    {
        public ArticleLogic()
        {

        }
        public ArticleLogic(DB4ODALClient client)
            : base(client)
        {

        }

        public Article GetById(long id)
        {
            return GetList(p => p.Id == id).FirstOrDefault();
        }

        /// <summary>
        /// 暂时没有用
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<Article> GetAllByParentId(long parentId)
        {
            return new List<Article>();
        }

        /// <summary>
        /// 获取相关文档
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="num">要取的数量</param>
        /// <returns></returns>
        public IEnumerable<Article> GetRelatedArticleList(int id, string keywords, int num)
        {
            List<Article> articleList = GetList();

            string parten = string.Format("{0}", string.Join("|", keywords.Split(',', '，', ' ')));

            int c = Regex.Matches(articleList[0].Content, parten).Count;

            return (from d in articleList where d.Id != id orderby Regex.Matches(d.Content, parten).Count descending select d).Take(num);
        }

        /// <summary>
        /// 自动获取正确的来源标题
        /// </summary>
        /// <param name="orgn">原始的来源，用户输入的信息</param>
        /// <param name="url">用户输入的来源网址</param>
        /// <returns></returns>
        public string GetArticleOrignSourceTitle(string orgn, string url)
        {
            string result = string.Empty;
            if (string.IsNullOrEmpty(orgn) && orgn != "本站")
            {
                result = orgn;
            }
            else
            {
                if (string.IsNullOrEmpty(url) == false && url.ToLower().Contains(BasicCache.SiteConfig.SiteUrl.ToLower().Replace("http://", "")))
                {
                    result = "本站";
                }
                else
                {
                    result = "网络转载";
                }
            }
            return result;
        }

        /// <summary>
        /// 获取文章来源url
        /// </summary>
        /// <param name="url">用户输入的来源地址</param>
        /// <returns></returns>
        public string GetArticleOrignSourceUrl(string url)
        {
            string result = string.Empty;
            if (string.IsNullOrEmpty(url))
            {
                result = BasicCache.SiteConfig.SiteUrl;
            }
            else
            {
                result = url;
            }

            return result.ChangeToHttpStart();
        }

        /// <summary>
        /// 获取文章的URL地址,带网站配置中设置的域名的
        /// </summary>
        /// <param name="a">文章</param>
        /// <returns></returns>
        public string GetURL(Article a)
        {
            Catalogue c = BasicCache.AllCatalogueList.FirstOrDefault(p => p.Id == a.CatalogueId);
            return GetURL(a, c);
        }

        /// <summary>
        /// 不带域名的URL地址，以“/”表示根目录的绝对路径
        /// </summary>
        /// <param name="a">文章</param>
        /// <returns></returns>
        public string GetURL2(Article a)
        {
            Catalogue c = BasicCache.AllCatalogueList.FirstOrDefault(p => p.Id == a.CatalogueId);
            return GetURL2(a, c);
        }

        /// <summary>
        /// 获取文章的url地址,带网站配置中设置的域名的
        /// </summary>
        /// <param name="a">文章</param>
        /// <param name="c">文章所属的栏目</param>
        /// <returns></returns>
        public string GetURL(Article a, Catalogue c)
        {
            if (BasicCache.SiteConfig.IsStaticEnable)
            {
                return GetStaticURL(a, c);
            }
            else
            {
                return GetDynamicURL(a);
            }
        }

        /// <summary>
        /// 不带域名的URL地址，以“/”表示根目录的绝对路径
        /// </summary>
        /// <param name="a">文章</param>
        /// <param name="c">文章所述的栏目</param>
        /// <returns></returns>
        public string GetURL2(Article a, Catalogue c)
        {
            if (BasicCache.SiteConfig.IsStaticEnable)
            {
                return GetStaticURL2(a, c);
            }
            else
            {
                return GetDynamicURL2(a);
            }
        }

        private string GetStaticURL(Article a, Catalogue c)
        {
            return string.Format("{0}{1}{2}/articels/{3}.html",
                                  BasicCache.SiteConfig.SiteUrl.ChangeToHttpStart(),
                                  System.Web.VirtualPathUtility.ToAbsolute("~/"),
                                  c.HtmlPath,
                                  a.Id);
        }

        private string GetDynamicURL(Article a)
        {
            return string.Format("{0}{1}ShowArticle.aspx?id={2}",
                                BasicCache.SiteConfig.SiteUrl.ChangeToHttpStart(),
                                System.Web.VirtualPathUtility.ToAbsolute("~/"),
                                a.Id);
        }

        private string GetStaticURL2(Article a, Catalogue c)
        {
            return string.Format("{0}{1}/articels/{2}.html",
                                  System.Web.VirtualPathUtility.ToAbsolute("~/"),
                                  c.HtmlPath,
                                  a.Id);
        }

        private string GetDynamicURL2(Article a)
        {
            return string.Format("{0}ShowArticle.aspx?id={1}",
                                System.Web.VirtualPathUtility.ToAbsolute("~/"),
                                a.Id);
        }
    }
}
